import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { DepositPage } from './deposit/deposit'
import { TransferPage } from './transfer/transfer'
import { WithdrawalPage } from './withdrawal/withdrawal'
import { TransactionPage } from './transaction/transaction';
import { HttpService } from '../../service/httpService';
import { UserService } from '../../service/userService';
@Component({
  selector: 'page-wallet',
  templateUrl: 'wallet.html',
})
export class WalletPage {

  private isLoad = false;
  private platforms = [];
  private refresh:any;
  private showLoading = true;
  constructor(public navCtrl: NavController,
              public http:HttpService,
              public user:UserService,
              public navParams: NavParams) {

  }

  doRefresh(refresh){
    this.refresh = refresh;
    this.getAllPlatform();
    this.getUserCash();
  }


  //加载完成
  ionViewWillEnter(){
    if (!this.isLoad && this.user.isLogin){
      this.getAllPlatform();
      this.getUserCash();
      this.isLoad = true;
    }
  }

  getUserCash(){
    let url = "/api/User/GetUserCash";
    let self = this;
    this.user.isGetCash = false;
    this.http.get(url,{},function (res,err) {
      if(!err){
        self.user.isGetCash = true;
        self.user.cash = res;
      }
    });
  }

  getAllPlatform(){
    let self = this;
    let url = "/api/Game/GetAllApi";
    this.http.get(url,{},function (res,err) {
      self.showLoading = false;
      if(!err){
        self.platforms = [];
        for(var i=0;i<res.length;i++){
          let obj = {
            name:res[i].GameName,
            code:res[i].GamePlatform,
            money:0.0,
            isLoading:true
          };
          self.getPlatformCash(res[i].GamePlatform);
          self.platforms.push(obj);
        }
      }
    })
  }
  getPlatformCash(platform){

    let url = "/api/Game/GetCash";
    let self = this;
    this.http.get(url,{gamePlatform:platform},function (res,err) {
      if (self.refresh){
        self.refresh.complete();
      }
      if(!err){
        for(var i=0;i<self.platforms.length;i++){
          let item = self.platforms[i];
          if(item.code == platform){
            item.money = res;
            item.isLoading = false;
          }
        }
        self.user.platformsCash = self.platforms;
      }
    })
  }

  gotoPageAction(index){

    if (index == 1){
      this.navCtrl.push(DepositPage);
    }else if(index == 2){
      this.navCtrl.push(TransferPage);
    }else if(index == 3){
      this.navCtrl.push(WithdrawalPage);
    }else if(index == 4){
      this.navCtrl.push(TransactionPage);
    }
  }


}
